<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入第一个数字" id="i1"> <br>
<input type="text" placeholder="请输入第二个数字" id="i2"> <br>
<button onclick="cale('+')">加</button>
<button onclick="cale('-')">减</button>
<button onclick="cale('*')">乘</button>
<button onclick="cale('/')">除</button>
<h4>运算结果为：<span></span></h4>

<script>
    function cale(o){
        let n1 = document.querySelector('#i1').value;
        let n2 = document.querySelector('#i2').value;
        n1=parseFloat(n1);
        n2=parseFloat(n2);
        let spanE = document.querySelector('span');
        switch (o){
            case '+':
                spanE.innerHTML = n1+n2; break;
            case '-':
                spanE.innerHTML = n1-n2; break;
            case '*':
                spanE.innerHTML = n1*n2; break;
            case '/':
                if(n2==0){
                    alert('除数不能等于0');
                }else{
                    spanE.innerHTML=n1/n2;
                }

        }
    }
</script>
</body>
</html>